using System;
using System.ComponentModel;
using System.Windows.Input;
using BHS.TFSChangsetAnalyser.Import;
using BHS.TFSChangsetAnalyser.Model;

namespace BHS.TFSChangsetAnalyser.ViewModel
{
    public class MainWindowViewModel : IMainWindowViewModel, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public TfsServerConnectionDetails TfsServerConnectionDetails { get; set; }

        public IChangesetInformationProvider Importer { get; private set; }

        public ChangesetAnalysisResults AnalysisResults { get; set; }

        public IDefaultValuesProvider DefaultValuesProvider { get; private set; }

        public ICommand AnalyseCommand { get; private set; }

        public ICommand ExportCommand { get; private set; }

        public ICommand SaveCommand { get; private set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set 
            { 
                _isBusy = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsBusy"));
                }
            }
        }

        public MainWindowViewModel(CommandFactory commandFactory,
                                   IDefaultValuesProvider defaultValuesProvider,
                                   TfsServerConnectionDetails tfsServerConnection,
                                   IChangesetInformationProvider importer)
        {
            DefaultValuesProvider = defaultValuesProvider;
            TfsServerConnectionDetails = tfsServerConnection;
            Importer = importer;
            Importer.ViewModel = this;

            AnalyseCommand = commandFactory.CreateCommand(typeof(AnalyseCommand), this);
            SaveCommand = commandFactory.CreateCommand(typeof(SaveDefaultsCommand), this);
            ExportCommand = commandFactory.CreateCommand(typeof(ExportCommand), this);

            AnalysisResults = new ChangesetAnalysisResults();
            DefaultValuesProvider.Initialize();

            ApplyDefaults();
            TfsServerConnectionDetails.ApplyDefaults(DefaultValuesProvider);
        }

        private void ApplyDefaults()
        {
            StartDate = DefaultValuesProvider.StartDate;
            EndDate = DefaultValuesProvider.EndDate;
        }
    }
}